Các ví dụ Visual Basic for Applications

Một công dụng phổ biến của VBA là thêm hàm vào giao diện ứng dụng chuẩn. Macro này chuẩn bị một shortcut để nhập ngày hiện tại trong Word:

1 Sub NhapNgayHienTai()2 ' NhapNgayHienTai Macro3 ' Macro recorded 15/03/2005 by UserName4 ' 5     Selection.InsertDateTime DateTimeFormat:="d-MMM-yy", InsertAsField:=False, _6          DateLanguage:=wdEnglishAUS, CalendarType:=wdCalendarWestern, _7         InsertAsFullWidth:=False8 End Sub

VBA rất có ích trong việc tự động cập nhật dữ liệu qua một bảng:

 1 Sub LoopTableExample 2  3     Dim db As DAO.Database 4     Dim rs As DAO.Recordset 5  6     Set db = CurrentDb 7     Set rs = db.OpenRecordset("SELECT * FROM tblMain") 8  9     Do Until rs.EOF10         MsgBox rs!FieldName11         rs.MoveNext12     Loop13 14     Set rs = Nothing15     Set db = Nothing16 End Sub

VBA có thể dùng để thêm hàm mới trong bảng tính Microsoft Excel:

 1 Public Function BusinessDayPrior(dt As Date) As Date 2  3     Select Case Weekday(dt, vbMonday) 4         Case 1 5             BusinessDayPrior = dt - 3      6         Case 7 7             BusinessDayPrior = dt - 2      8         Case Else 9             BusinessDayPrior = dt - 1     10     End Select11 End Function